package maven.jyhs.com.util;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

public class Reflection {

    public static  void   mapToObject(Map<String, String> map, Object object) throws IllegalAccessException {
        Class<?> targetClass = object.getClass();
        Field[]  fields= targetClass.getDeclaredFields();
        for (Field field : fields ){
            field.setAccessible(true);
            String typeName=field.getType().getTypeName();
             if (map.get(field.getName())!=null){
                 if ("java.lang.Long".equals(typeName)){
                     Long longValue =Long.valueOf(map.get(field.getName()));
                     field.set(object,longValue);
                 }else if ("java.math.BigDecimal".equals(typeName)){
                     BigDecimal bigDecimal =new BigDecimal(map.get(field.getName()));
                     field.set(object,bigDecimal);
                 }else if ("java.util.Date".equals(typeName)||"java.time.LocalDateTime".equals(typeName)){
                     SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd");
                     field.set(object,formatter);
                 } else if ("java.lang.Integer".equals(typeName)||"int".equals(typeName)) {
                     // 注意这里可能需要额外的错误处理，如NumberFormatException
                     Integer intValue = Integer.valueOf(map.get(field.getName()));
                     field.set(object, intValue);
                 }else {
                     field.set(object,map.get(field.getName()));
                 }
             }
        }
    }

    public  static  void httpServletToObject(HttpServletRequest req, Object obj) throws IllegalAccessException, ParseException {
        Class<?> targeClass = obj.getClass();
        Field[] fields = targeClass.getDeclaredFields();
        for(Field f:fields){
            f.setAccessible(true);
            String typeName = f.getType().getSimpleName();
            if (null!=req.getParameter(f.getName())&&!"".equals(req.getParameter(f.getName()))){
                if ("java.lang.Integer".equals(typeName)||"Integer".equals(typeName)){
                    f.set(obj,Integer.valueOf(req.getParameter(f.getName())));
                }else if ("java.lang.String".equals(typeName)){
                    f.set(obj,req.getParameter(f.getName()));
                }else if ("Date".equals(typeName)||"java.util.Date".equals(typeName)){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    java.util.Date date = sdf.parse(req.getParameter(f.getName()));
                    f.set(obj,date);
                }else {
                    f.set(obj,req.getParameter(f.getName()));
                }
            }
        }
    }

    public  static  void ReflectUtil(ResultSet rs, Object obj) throws IllegalAccessException, SQLException {
        Class<?> targeClass = obj.getClass();
        Field[] fields = targeClass.getDeclaredFields();
        for(Field f:fields){
            f.setAccessible(true);
            String typeName = f.getType().getName();
            if (null!=rs.getObject(f.getName())){
                if ("java.lang.Integer".equals(typeName)){
                    f.set(obj,Integer.valueOf(rs.getInt(f.getName())));
                }else if ("java.lang.String".equals(typeName)){
                    f.set(obj,rs.getString(f.getName()));
                }else if ("java.util.Date".equals(typeName)||"java.time.LocalDateTime".equals(typeName)){
//                    ZoneId zoneId = ZoneId.systemDefault();
//                    Date from = Date.from(((LocalDateTime) rs.getObject(f.getName())).atZone(zoneId).toInstant());
//                    System.out.println("时间"+from);
                    Date date = rs.getDate(f.getName());
                    System.out.println(date);
                    f.set(obj, date);
                }else {
                    f.set(obj,rs.getObject(f.getName()));
                }
            }
        }
    }



}
